package edu.ucsd.cse110;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import edu.ucsd.cse110.model.Wod;

/**
 * Displays the name, composition, and rating of the WOD.
 */
public class BenchmarkDetailActivity extends Activity implements
		OnClickListener, OnRatingBarChangeListener {
	private Activity mActivity;
	private LocalDatabaseManager mDatabase;
	private Wod mWod, item;
	private int mPosition;
	private Wod.Type mWodType;
	private EditText editName, editDescription;
	private TextView editRadioMessage, txtName, txtExercise;
	private RatingBar rating;

	/**
	 * Handles buttons onclick events.
	 */
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.view_graph_button:
			StatsWodGraph line = new StatsWodGraph(mWod.getName(), mWod.getId());
			Intent lineIntent = line.getIntent(this);
			startActivity(lineIntent);
			break;
		case R.id.view_details_button:
			Bundle data = new Bundle();
			data.putString("wodString", mWod.getName());
			data.putInt("id", mWod.getId());
			startActivity(new Intent(this, StatsWodDetailActivity.class)
					.putExtras(data));
			break;
		case R.id.edit_custom_wod_button:
			LayoutInflater factory = LayoutInflater.from(this);
			View view = factory.inflate(R.layout.customwod_edit, null);

			mDatabase = new LocalDatabaseManager(this);
			mWod = mDatabase.getWodById(getIntent().getExtras().getInt("Id"))
					.get(0);

			// Initializes alert dialog.
			AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);

			// Changes the layout of the dialog.
			myAlertDialog.setTitle(mWod.getName().toString());
			myAlertDialog.setView(view);

			editName = (EditText) view.findViewById(R.id.custom_input_name);
			editName.setText(mWod.getName().toString());

			editDescription = (EditText) view
					.findViewById(R.id.custom_input_description);
			editDescription.setText(mWod.getDescription().toString());

			editRadioMessage = (TextView) view.findViewById(R.id.radio_message);
			editRadioMessage
					.setText("Cannot change for time, reps, and rounds to keep stats consistent.");

			myAlertDialog.setPositiveButton("OK",
					new DialogInterface.OnClickListener() {
						/**
						 * Updates an existing custom WOD when OK button is
						 * pressed.
						 */
						public void onClick(DialogInterface dialog, int which) {
							item = new Wod();
							item.setName(editName.getText().toString());
							item.setDescription(editDescription.getText()
									.toString());
							item.setType(Wod.Type.Custom);
							item.setPRType(mDatabase
									.getWodById(
											getIntent().getExtras()
													.getInt("Id")).get(0)
									.getPRType());
							item.setId(getIntent().getExtras().getInt("Id"));
							item.setRating(mDatabase
									.getWodById(
											getIntent().getExtras()
													.getInt("Id")).get(0)
									.getRating());
							mDatabase.updateWod(item);
							mDatabase.close();

							// update the name and text of the custom Wod
							txtName.setText(item.getName());
							txtExercise.setText(item.getDescription());

							Intent data = new Intent();
							data.putExtra("Position", mPosition);
							data.putExtra("Rating", item.getRating());
							data.putExtra("Name", item.getName().toString());
							data.putExtra("Description", item.getDescription()
									.toString());
							mActivity.setResult(RESULT_OK, data);
							return; // TODO: Remove to see effect.
						}
					});
			myAlertDialog.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						/**
						 * Does nothing when Cancel button is clicked.
						 */
						public void onClick(DialogInterface dialog, int which) {
							return; // TODO: Remove to see effect.
						}
					});
			myAlertDialog.show();
			break;
		default:
			// TODO: Remove to see effect.
			break;
		}
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.benchmarkdetail);

		mActivity = this;

		// Gets the arguments passed into this activity.
		Bundle extras = getIntent().getExtras();
		mPosition = extras.getInt("Position");
		mWodType = Wod.Type.values()[extras.getInt("Type")];

		mDatabase = new LocalDatabaseManager(this);
		mWod = mDatabase.getWodById(extras.getInt("Id")).get(0);
		mDatabase.close();

		if (mWodType == Wod.Type.Custom) {
			Button btnEdit = (Button) findViewById(R.id.edit_custom_wod_button);
			btnEdit.setVisibility(0);// Make this button Visible
			btnEdit.setOnClickListener(this);
		}

		// Sets the name of the WOD.
		txtName = (TextView) findViewById(R.id.benchmarkdetail_txt_name);
		txtName.setText(mWod.getName());

		// Sets the description of the WOD.
		txtExercise = (TextView) findViewById(R.id.benchmarkdetail_txt_exercises);
		txtExercise.setText(mWod.getDescription());

		// Sets the rating of the WOD.
		rating = (RatingBar) findViewById(R.id.benchmarkdetail_rating);
		rating.setRating(mWod.getRating());
		rating.setOnRatingBarChangeListener(this);

		/*
		 * Displaying quick-link buttons that take user to STATS from WOD list
		 * only if a logbook entry exists for that WOD. Otherwise, no buttons
		 * will be made available to user.
		 */
		mDatabase = new LocalDatabaseManager(this);
		int wodId = mWod.getId();
		if (mDatabase.getLogByWodId(wodId).size() > 0) {
			Button graphButton = (Button) findViewById(R.id.view_graph_button);
			Button detailsButton = (Button) findViewById(R.id.view_details_button);
			View sBar = findViewById(R.id.statsBar);
			sBar.setVisibility(0);
			graphButton.setOnClickListener(this);
			detailsButton.setOnClickListener(this);
		}
		mDatabase.close();
	}

	/**
	 * Updates the rating of a WOD in the database and in the list.
	 */
	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		Wod newWod = mWod;
		newWod.setRating((int) rating);

		mDatabase = new LocalDatabaseManager(mActivity);
		mDatabase.updateWod(newWod);
		mDatabase.close();

		Intent data = new Intent();
		data.putExtra("Position", mPosition);
		data.putExtra("Rating", (int) rating);

		mActivity.setResult(RESULT_OK, data);
	}
}